package com.xiaoc.bulldozer.facade.impl;

import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.dto.WeixinUserDto;
import com.xiaoc.bulldozer.common.dto.WeixinUserQueryDto;
import com.xiaoc.bulldozer.common.model.WeixinUser;
import com.xiaoc.bulldozer.common.service.WeixinUserService;
import com.xiaoc.bulldozer.common.util.SessionIdGenerateUtil;
import com.xiaoc.bulldozer.facade.WeixinUserFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * describe:
 *
 * @author lihuancheng
 * @date 2018/9/12
 */
@Service("weixinUserFacade")
public class WeixinUserFacadeImpl implements WeixinUserFacade {

    @Resource(name = "weixinUserService")
    private WeixinUserService weixinUserService;

    @Override
    public void updateWeixinUser(WeixinUser weixinUser) {
        WeixinUser existWeixinUser = weixinUserService.getWeixinUserById(weixinUser.getId());

        existWeixinUser.setUserType(weixinUser.getUserType());
        existWeixinUser.setUpdateTime(System.currentTimeMillis());
        weixinUserService.update(existWeixinUser);

        WeixinUserDto weixinUserDto = BeanUtil.copyProperties(WeixinUserDto.class, existWeixinUser);

        String sessionId = SessionIdGenerateUtil.generateSessionId(existWeixinUser.getOpenId());

        weixinUserService.putWeixinUserDto(sessionId, weixinUserDto);
    }

    @Override
    public List<WeixinUserDto> getWeixinUserList(WeixinUserQueryDto weixinUserQueryDto) {

        List<WeixinUserDto> weixinUserDtoList = weixinUserService.getWeixinUserList(weixinUserQueryDto);
        if (CollectionUtils.isNotEmpty(weixinUserDtoList)) {
            for (WeixinUserDto weixinUserDto : weixinUserDtoList) {
                switch (weixinUserDto.getUserType().intValue()) {
                    case 1:
                        weixinUserDto.setUserTypeName("普通用户");
                        break;
                    case 2:
                        weixinUserDto.setUserTypeName("注册用户");
                        break;
                    case 3:
                        weixinUserDto.setUserTypeName("渠道经理");
                        break;
                    case 100:
                        weixinUserDto.setUserTypeName("分享达人");
                        break;
                    default:
                        break;
                }
            }
        }
        return weixinUserDtoList;
    }

    @Override
    public Integer getWeixinUserCount(WeixinUserQueryDto weixinUserQueryDto) {
        return weixinUserService.getWeixinUserCount(weixinUserQueryDto);
    }
}
